package com.itlong.cloud.controller.value;

import com.fasterxml.jackson.core.type.TypeReference;
import com.itlong.cloud.POJO.DTO.operate.OperateModuleGetPageDTO;
import com.itlong.cloud.POJO.DTO.operate.OperatePropertySaveDeployDTO;
import com.itlong.cloud.POJO.DTO.operate.OperatePropertyValueSaveDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateUserInfoGetPageDTO;
import com.itlong.cloud.POJO.PO.OperateUserInfoPO;
import com.itlong.cloud.POJO.PO.ProjectPO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.OperateModuleGetPageVO;
import com.itlong.cloud.POJO.VO.operate.OperatePropertyValueGetOneVO;
import com.itlong.cloud.POJO.VO.operate.OperateUserInfoGetOneVO;
import com.itlong.cloud.POJO.VO.operate.OperateUserInfoGetUserPageVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.OperateErrorCodeEnum;
import com.itlong.cloud.project.operate.IOperateProjectServiceClient;
import com.itlong.cloud.project.operate.IOperatePropertyValueServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.user.operate.IOperateUserInfoServiceClient;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.node.JsonUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      运营-物业增值-物业增值管理
 * </desc>
 *
 * @createDate 2017/09/28
 */
@RestController
@RequestMapping("/propertyValue/module")
public class OperatePropertyValueManageController extends BaseController{

    @Autowired
    IOperatePropertyValueServiceClient iOperatePropertyValueServiceClient;

    @Autowired
    IOperateProjectServiceClient iOperateProjectServiceClient;

    /**
     * <desc>
     *      获取分页列表
     * </desc>
     *
     * @param dto
     * @return
     * @author Jiaqi.X
     * @createDate 2019/05/29
     */
    @RequestMapping(path = "list",method = RequestMethod.POST)
    @ResponseBody
    @AuthAccessRequired
    public Object list(OperateModuleGetPageDTO dto){
        MessageVO messageVO;
        try{
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            dto= HttpProtocolUtil.parseRequestParamToDTO(OperateModuleGetPageDTO.class,request);
            Page<OperateModuleGetPageVO> page=iOperatePropertyValueServiceClient.getPage(dto);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);
        }catch (Exception e){
            throw new DataAccessException("【运营_物业增值_物业增值管理list】获取分页列表失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      获取分页列表
     * </desc>
     *
     * @param moduleId
     * @return
     * @author Jiaqi.X
     * @createDate 2019/05/29
     */
    @RequestMapping(path = "getOne",method = RequestMethod.POST)
    @ResponseBody
    public Object getOne(String moduleId,String projectId){
        MessageVO messageVO;
        try{
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            moduleId= request.getAttribute("moduleId")==null?null:request.getAttribute("moduleId").toString();
            projectId= request.getAttribute("projectId")==null?null:request.getAttribute("projectId").toString();
            OperatePropertyValueGetOneVO vo=iOperatePropertyValueServiceClient.getOne(moduleId,projectId);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),vo);
        }catch (Exception e){
            throw new DataAccessException("【运营_物业增值_获取分页列表getOne】获取分页列表失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      保存物业增值数据
     * </desc>
     *
     * @param jsonStr
     * @return
     * @author Jiaqi.X
     * @createDate 2019/05/29
     */
    @RequestMapping(path = "save",method = RequestMethod.POST)
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.UPDATE_PROPERTY_VALUE_DEPLOY_LOG_DESC)
    public Object save(String jsonStr,OperatePropertyValueSaveDTO dto){
        MessageVO messageVO;
        try{
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            jsonStr= request.getAttribute("jsonStr")==null?null:request.getAttribute("jsonStr").toString();
            dto=HttpProtocolUtil.parseRequestParamToDTO(OperatePropertyValueSaveDTO.class,request);
            //收费模式和物业增值不能同时开启
            ProjectPO oldProject = iOperateProjectServiceClient.getOneProject(dto.getProjectId());
            if(dto.getPropertyValue() != null && dto.getPropertyValue().equals("1") && oldProject.getChargeMode()==2){
                return new MessageVO(OperateErrorCodeEnum.PROJECT_FUNCITON_ISNOT_ALLOWD.getErrorCode());
            }
            dto.setArrearsLevels(JsonUtil.decode(jsonStr, new TypeReference<List<OperatePropertySaveDeployDTO>>(){}));
            String result = iOperatePropertyValueServiceClient.save(dto);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_物业增值_保存save】保存物业增值数据失败",e);
        }
        return messageVO;
    }

}
